<?php

use yii\helpers\Html;
use yii\grid\GridView;
use common\business\EhrHelper;
use yii\helpers\ArrayHelper;
use kartik\tree\TreeView;
use leandrogehlen\treegrid\TreeGrid;

/* @var $this yii\web\View */
/* @var $searchModel \common\business\kd\KdPersonSearch */
$this->registerJsFile('/js/zTree_v3-master/js/jquery.ztree.all.js', [
    'depends' => 'backend\assets\AppAsset',
]);
$this->registerCssFile('/js/zTree_v3-master/css/zTreeStyle/zTreeStyle.css', [
    'depends' => 'backend\assets\AppAsset',
]);
//$this->registerCssFile('/js/zTree_v3-master/css/demo.css', [
//    'depends' => 'backend\assets\AppAsset',
//]);

\backend\assets\DataTableAsset::register($this);

$this->title = Yii::t('app', 'Kingdee Orgs');
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="company-index">

    <h1><?= Html::encode($this->title) ?></h1>
    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>

    <p>
        <?= Html::a(Yii::t('app', 'Create Company'), ['create'], ['class' => 'btn btn-success']) ?>
        <?= Html::a(Yii::t('app', 'Sync Company from HR'), ['sync-from-hr'], ['class' => 'btn btn-primary', 'target'=>'_blank']) ?>
    </p>
    <?php
    try {

    } catch (Exception $e) {
    }
    ?>
</div>
<div class="row">
    <div class="col-md-4 col-sm-4 col-xs-4">

        <div class="box box-info">
            <div class="box-header with-border">
                <h3 class="box-title">部门树</h3>

                <div class="box-tools pull-right">
                    <button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i>
                    </button>
                    <button type="button" class="btn btn-box-tool" data-widget="remove"><i class="fa fa-times"></i></button>
                </div>
            </div>
            <!-- /.box-header -->
            <div class="box-body">
                <ul id="treeDemo" class="ztree"></ul>
                <div class="overlay">
                    <i class="fa fa-refresh fa-spin"></i>
                </div>
            </div>
            <!-- /.box-body -->
            <div class="box-footer clearfix">
                <div class="btn-group">
                    <button type="button" class="btn btn-default"><i class="fa fa-plus"></i></button>
                    <button type="button" class="btn btn-default"><i class="fa fa-align-center"></i></button>
                    <button type="button" class="btn btn-default"><i class="fa fa-align-right"></i></button>
                </div>
            </div>
            <!-- /.box-footer -->
        </div>
    </div>
    <div class="col-md-8 col-sm-8 col-xs-8">
        <div class="box">
            <div class="box-header">
                <h3 class="box-title"><?= Yii::t('app', 'Employees')?></h3>
                <div class="box-tools">
                    <ul class="pagination pagination-sm no-margin pull-right">
                        <li><a id="dt_refresh_a" href="#"><i class="fa fa-refresh"></i></a></li>
                    </ul>
                </div>
            </div>
            <!-- /.box-header -->
            <div class="box-body">
                <table id="example" class="table table-bordered table-hover">
                    <thead>
                    <tr>
                        <th>Id</th>
                        <th>Name</th>
                        <th>Number</th>
                        <th>Gender</th>
                        <th>Birthday</th>
                        <th>Email</th>
                        <th>Mobile</th>
                        <th>id_card_num</th>
                        <th>native_place</th>
                        <th>blood_type</th>
                        <th>wed_status</th>
                        <th>nationality</th>
                        <th>folk</th>
                        <th>hklx</th>
                        <th>weixin</th>
                        <th>position_name</th>
                        <th>position_number</th>
                        <th>labor_relation_state</th>
                        <th>department_name</th>
                        <th>department_number</th>
                        <th>department_display_name</th>
                    </tr>
                    </thead>
                    <tbody>
                    </tbody>
                    <tfoot>
                    <tr>
                    </tr>
                    </tfoot>
                </table>
            </div>
            <!-- /.box-body -->
        </div>
<!--        --><?php // echo $this->render('_ajaxIndex', [
//            'searchModel' => $searchModel,
//            'dataProvider' => $dataProvider,
//            'models' => $models,
//        ]); ?>
    </div>
</div>

<script>
    <?php $this->beginBlock('js_running')?>

    var fid;
    var dt;

    var zTreeObj;
    // zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
    var setting = {
        async: {
            enable: true,
            url: "/department/ajax-department",
            autoParam:["id", "name", "number"]
        },
        check : {
            enable: true,
            chkStyle: "checkbox",
            chkboxType: { "Y": "ps", "N": "ps" }
        },
        callback : {
            onAsyncSuccess: function (event, treeId, treeNode, msg) {
                // alert(msg);
                $('.overlay').html('');
            },
            onClick: function (event, treeId, treeNode) {
                // alert(treeNode.tId + ", " + treeNode.id);
                fid = treeNode.id;
                // var oSettings = dt.fnSettings();
                // oSettings.sAjaxSource = "/kd/ajax-get-list-by-org?id=" + fid;//重新设置url
                //Nothing will happen

                // alert(dt.ajax.url);
                dt.ajax.url("/kd/ajax-get-list-by-org?id=" + fid).load();//重新设置url
                // dt.fnDraw();
                // dt = loadDataTable(fid);
                // dt.ajax.reload();
            }
        }
    };

    function loadDataTable(fid) {
        dt = $('#example').DataTable({
            "paging":   true,
            'aLengthMenu': [[10, 20, 50], ['10', '20', '50']],  //设置每页显示记录的下拉菜单
            "ordering": true,
            "info":     true,
            "scrollX": true,
            "processing": true,
            "serverSide": true,
            'stateSave' : true,
            // "searching": false,
            "ajax": {
                "url" : "/kd/ajax-get-list-by-org?id=" + fid,
                'type': 'get'
            },
            'language': {
                "processing": "正在处理中。。。",
                "info": "正在显示第 _START_ 条至第 _END_ 条数据， 总共 _TOTAL_ 条数据",
                // "infoEmpty": "正在显示第 0 条至第 0 条数据， 总共 0 条数据",
                "paginate": {
                    "first": "第一页",
                    "last": "最后一页",
                    "next": "下一页",
                    "previous": "上一页",
                    "search": "过滤记录:"
                },
                'lengthMenu' : '显示 _MENU_ 项结果'
            },
            "order": [[1, 'asc']],
            "columnDefs": [
                { "visible": false,  "targets": [ 0 ] },
                {
                    // 第1列为name
                    "render": function ( data, type, row ) {
                        // return data +' ('+ row[3]+')';
                        return "<a href=\"/kingdee/person/view?id="+encodeURIComponent(row['fid'])+"\" target=\"_blank\">"+data+"</a>";
                    },
                    "targets": 1
                }
            ],
            "columns": [
                { "data": "fid", 'title' : 'id号' },
                { "data": "name", 'title' : '姓名', 'width' : '30px' },
                { "data": "number", 'title' : '员工号' },
                {
                    "data": function ( row, type, val, meta ) {
                        return row.gender == 1 ? '男' : '女';
                    },
                    'title' : '性别' },
                { "data": "birthday", 'title' : '生日', 'width' : '50px' },
                { "data": "email", 'title' : '邮箱' },
                { "data": "mobile", 'title' : '手机' },
                { "data": "id_card_num", 'title' : '身份证号' },
                { "data": "native_place", 'title' : '家乡' },
                { "data": function ( row, type, val, meta ) {
                        switch (row.blood_type) {
                            case '0':
                                return '未知';
                            case '10':
                                return 'A';
                            case '20':
                                return 'B';
                            case '30':
                                return 'AB';
                            case '40':
                                return 'O';
                            case '90':
                                return '其他';
                            default:
                                return '其他';
                        }
                    },
                    'title' : '血型' },
                { "data": "wed_status", 'title' : '婚姻状况' },
                { "data": "nationality", 'title' : '国籍' },
                { "data": "folk", 'title' : '民族' },
                { "data": "hklx", 'title' : '户口类型' },
                { "data": "weixin", 'title' : '微信' },
                { "data": "position_name", 'title' : '职位名称' },
                { "data": "position_number", 'title' : '职位编号' },
                { "data": "labor_relation_state", 'title' : '用工关系状态' },
                { "data": "department_name", 'title' : '部门名称' },
                { "data": "department_number", 'title' : '部门编号' },
                { "data": "department_display_name", 'title' : '部门全称', 'width' : '100px' }
            ]
        });
        return dt;
    }

    $(document).ready(function() {
        // zNodes = [
        //     {name:"neil1", open:true, children:[
        //             {name:"test1_1"}, {name:"test1_2"}]},
        //     {name:"neil2", open:true, children:[
        //             {name:"test2_1"}, {name:"test2_2"}]}
        // ];

        // zTreeObj = $.fn.zTree.init($("#treeDemo"), setting, zNodes);
        zTreeObj = $.fn.zTree.init($("#treeDemo"), setting);

        // // Setup - add a text input to each footer cell
        // $('#example tfoot th').each( function () {
        //     var title = $(this).text();
        //     $(this).html( '<input type="text" placeholder="Search '+title+'" />' );
        // } );

        dt = loadDataTable('');
        // dt.on( 'search.dt', function () {
        //     $('#filterInfo').html( 'Currently applied global search: '+table.search() );
        // } );

        $('#dt_refresh_a').click(function(){
            dt.ajax.reload();
        });
    });
    <?php $this->endBlock()?>
</script>
<?php
$this->registerJs($this->blocks['js_running'], \yii\web\View::POS_END)
?>
